home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / deltablu / cnstrnts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-17  |  3.9 KB  |  189 lines

  1. /***************************************************************************
  2.  Constraints.c
  3.  
  4.     Constraint, variable, and other operations for DeltaBlue.
  5.  
  6. ****************************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "List.h"
  12. #include "Constraints.h"
  13. #include "DeltaBlue.h"
  14.  
  15. /******* Private *******/
  16.  
  17. void Error(char*);
  18. static void Error(errorString)
  19. char* errorString;
  20. {
  21.     printf("Constraints.c error: %s.\n", errorString);
  22.     exit(-1);
  23. }
  24.  
  25. void Execute(Constraint);
  26. static void Execute(c)
  27. Constraint c;
  28. {
  29.     c->execute(c);
  30. }
  31.  
  32. void Noop(Constraint);
  33. static void Noop(c)
  34. Constraint c;
  35. {
  36.     /* default execute procedure; does nothing */
  37. };
  38.  
  39. /******* Variables *******/
  40.  
  41. Variable Variable_Create(name, initialValue)
  42. char *name;
  43. long initialValue;
  44. {
  45.     register Variable new;
  46.  
  47.     new = (Variable) malloc(sizeof(VariableStruct));
  48.     if (new == NULL) Error("out of memory");
  49.     new->value = initialValue;
  50.     new->constraints = List_Create(2);
  51.     new->determinedBy = NULL;
  52.     new->mark = 0;
  53.     new->walkStrength = S_weakest;
  54.     new->stay = true;
  55.     strncpy(new->name, name, 10);
  56.     new->name[9] = 0;
  57.     AddVariable(new);
  58.     return new;
  59. }
  60.  
  61. Variable Variable_CreateConstant(name, value)
  62. char *name;
  63. long value;
  64. {
  65.     register Variable new;
  66.  
  67.     new = (Variable) malloc(sizeof(VariableStruct));
  68.     if (new == NULL) Error("out of memory");
  69.     new->value = value;
  70.     new->constraints = List_Create(0);
  71.     new->determinedBy = NULL;
  72.     new->mark = 0;
  73.     new->walkStrength = S_required;
  74.     new->stay = true;
  75.     strncpy(new->name, name, 10);
  76.     new->name[9] = 0;
  77.     AddVariable(new);
  78.     return new;
  79. }
  80.  
  81. void Variable_Destroy(v)
  82. Variable v;
  83. {
  84.     if (v->constraints == NULL) {
  85.     Error("bad VariableStruct; already freed?");
  86.     }
  87.     List_Destroy(v->constraints);
  88.     v->constraints = NULL;
  89.     free(v);
  90. }
  91.  
  92. void Variable_Print(v)
  93. Variable v;
  94. {
  95.     printf(
  96.     "%s(%s,%ld)",
  97.     v->name, StrengthString(v->walkStrength), v->value);
  98. }
  99.  
  100. /******* Constraints *******/
  101.  
  102. Constraint Constraint_Create(variableCount, strength)
  103. int variableCount, strength;
  104. {
  105.     register Constraint new;
  106.     int i;
  107.  
  108.     new = (Constraint) malloc(sizeof(ConstraintStruct) + ((variableCount - 1) * sizeof(Variable)));
  109.     if (new == NULL) Error("out of memory");
  110.     new->execute = Noop;
  111.     new->inputFlag = false;
  112.     new->strength = strength;
  113.     new->whichMethod = NO_METHOD;
  114.     new->methodCount = 0;
  115.     for (i = 0; i < 7; i++) {
  116.         new->methodOuts[i] = 0;
  117.     }
  118.     new->varCount = variableCount;
  119.     for (i = 0; i < new->varCount; i++) {
  120.         new->variables[i] = NULL;
  121.     }
  122.     return new;
  123. }
  124.  
  125. void Constraint_Destroy(c)
  126. Constraint c;
  127. {
  128.     if (c->execute == NULL) {
  129.     Error("bad ConstraintStruct; already freed?");
  130.     }
  131.     c->execute = NULL;
  132.     free(c);
  133. }
  134.  
  135. void Constraint_Print(c)
  136. Constraint c;
  137. {
  138.     int i, outIndex;
  139.  
  140.     if (!SATISFIED(c)) {
  141.     printf("Unsatisfied(");
  142.     for (i = 0; i < c->varCount; i++) {
  143.         Variable_Print(c->variables[i]);
  144.         printf(" ");
  145.     }
  146.     printf(")");
  147.     } else {
  148.     outIndex = c->methodOuts[c->whichMethod];
  149.     printf("Satisfied(");
  150.     for (i = 0; i < c->varCount; i++) {
  151.         if (i != outIndex) {
  152.         Variable_Print(c->variables[i]);
  153.         printf(" ");
  154.         }
  155.     }
  156.     printf("-> ");
  157.     Variable_Print(c->variables[outIndex]);
  158.     printf(")");
  159.     }
  160.     printf("\n");
  161. }
  162.  
  163. /******* Miscellaneous Functions *******/
  164.  
  165. char* StrengthString(strength)
  166. int strength;
  167. {
  168.     static char temp[20];
  169.  
  170.     switch (strength) {
  171.     case S_required:        return "required";
  172.     case S_strongPreferred:    return "strongPreferred";
  173.     case S_preferred:        return "preferred";
  174.     case S_strongDefault:    return "strongDefault";
  175.     case S_default:        return "default";
  176.     case S_weakDefault:        return "weakDefault";
  177.     case S_weakest:        return "weakest";
  178.     default:
  179.     sprintf(temp, "strength[%d]", strength);
  180.     return temp;
  181.     }
  182. }
  183.  
  184. void ExecutePlan(list)
  185. List list;
  186. {
  187.     List_Do(list, Execute);
  188. }
  189.